package com.itheima.controller;

import com.itheima.bean.User;
import com.itheima.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/*@Controller
@ResponseBody*/
@RestController  //等价于 @Controller+@ResponseBody
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;
/*
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        userService = ac.getBean("userService", UserService.class);
    }*/
    /**
     * 处理登录的请求
     */
    //@RequestMapping("/login")
    @PostMapping("/login")
    public boolean login(@RequestBody User user, HttpSession session) {
        //2 调用service层的login方法，得到新的user对象
        User existUser=userService.login(user);
        //3 判断如果user!=null,说明登录成功，将user保存到session
        if(existUser!=null){
            //登录成功
            session.setAttribute("user",existUser);
        }
        return existUser!=null;  //true表示登录成功，false表示登录失败
    }
}
